import * as types from '../constants/eventTypes'
import { Post } from '../../utils'

const initState = {
  type: '',
  page: 1,
  size: 20,
  curNews: {},
  newsList: []
}
const state = initState

const mutations = {
  [types.ON_CHANGE_NEWS_TYPE] (state, payload) {
    state.type = payload.type || ''
    state.page = 1
    state.events = []
  },
  [types.FETCH_EVENT_LIST] (state, payload) {
    state.page += 1
    state.events = state.events.concat(payload.data.array)
  },
  [types.FETCH_EVENT_DETAIL] (state, payload) {
    state.curEvent = payload.data
  }
}

const actions = {
  /**
   * 获取活动列表
   */
  fetchEventList ({commit, state}) {
    let params = {
      city: state.city,
      page: state.page,
      size: state.size
    }
    return new Promise((resolve, reject) => {
      Post('/api/activity/event/list', params).then(
        response => commit({
          type: types.FETCH_EVENT_LIST,
          data: response
        }),
        error => commit({
          type: types.FETCH_EVENT_LIST_FAILED,
          error
        })
      )
    })
  },
  /**
   * 获取活动详情
   */
  fetchEventDetail ({commit, state}, data) {
    Post('/api/activity/one', data).then(
      response => commit({
        type: types.FETCH_EVENT_DETAIL,
        data: response
      }),
      error => commit({
        type: types.FETCH_EVENT_DETAIL_FAILED,
        error
      })
    )
  }
}

export default {
  state,
  mutations,
  actions
}
